//
//  AlamofireTool.swift
//  06-Alamofire
//
//  Created by monkey on 2019/3/19.
//  Copyright © 2019 itcast. All rights reserved.
//

import Alamofire

class AlamofireTool
{
    /// 发送网络请求
    /// - Parameters:
    ///   - method: 方法类型
    ///   - urlString: 接口地址
    ///   - parameters: 参数
    ///   - encoding: 编码
    ///   - headers: 请求头
    ///   - completionHandle: 完成回调
    open class func request(method: HTTPMethod,
                            urlString: String,
                            parameters: [String: Any]? = nil,
                            encoding: ParameterEncoding = URLEncoding.default,
                            headers: [String: String]? = nil,
                            completionHandle: @escaping (Any?,Error?) -> Void) -> Void
    {
        let headersObject = HTTPHeaders(headers ?? [:])
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
        AF.request(urlString, method: method, parameters: parameters, encoding: encoding, headers: headersObject).responseJSON { (response) in
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            switch response.result {
            case .success(let value):
                completionHandle(value,nil)
            case .failure(let error):
                completionHandle(nil,error)
            }
        }.responseString { (response) in
//            XGPrint(response.request?.url)
//            XGPrint(response.result.value)
        }
    }
    
    /// 上传文件
    /// - Parameters:
    ///   - urlString: 接口地址
    ///   - fileData: 文件二进制数据
    ///   - fieldName: 文件字段名
    ///   - parameters: 参数
    ///   - headers: 请求头
    ///   - maxFileSize: 文件大小限制 默认5 * 1024 * 1024
    ///   - uploadProgressHandle: 上传进度回调
    ///   - completionHandle: 完成回调
    open class func upload(urlString: String,
                         fileData: Data,
                         fieldName: String,
                         parameters: [String: Any]? = nil,
                         headers: [String: String]? = nil,
                         maxFileSize: UInt64 = 5 * 1024 * 1024,
                         uploadProgressHandle: ((Double) -> Void)? = nil,
                         completionHandle: @escaping (Any?,Error?) -> Void
                         ) -> Void
    {
        let headersObject = HTTPHeaders(headers ?? [:])
        AF.upload(multipartFormData: { (multipartFormData) in
            // 拼接文件数据 带mimeType是拼接文件的方法
            multipartFormData.append(fileData, withName: fieldName, fileName: "xxx", mimeType: "application/octet-stream")
            // 拼接请求参数数据 不带mimeType的方法是拼接二进制参数的方法
            if let parameters = parameters {
                for (key,value) in parameters {
                    if let strData = (value as? String)?.data(using: .utf8) {
                        multipartFormData.append(strData, withName: key)
                    }
                }
            }
        }, to: urlString, usingThreshold: maxFileSize, method: .post, headers: headersObject).uploadProgress(queue: DispatchQueue.main, closure: { (progress) in
            uploadProgressHandle?(progress.fractionCompleted)
        }).responseJSON { (response) in
            switch response.result {
            case .success(let value):
                completionHandle(value,nil)
            case .failure(let error):
                completionHandle(nil,error)
            }
        }
    }
}
